package servlet.rool;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import pojo.Menu;
import service.MenuService;
import service.impl.MenuServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 新增管理员前回显的servlet
 */
@WebServlet(value = "/RoleAddEchoServlet")
public class RoleAddEchoServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        MenuService ms=new MenuServiceImpl();
        List<Menu> menus=ms.queryMenu();
        request.setAttribute("menus",menus);

        //转换JSON格式
        JSONObject json=new JSONObject();
        json.put("menus", JSONArray.fromObject(menus));
        request.setAttribute("jsonmenus",json);
        request.getRequestDispatcher("jsp/yjj/RoleAdd.jsp").forward(request,response);
    }
    
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
        doGet(request, response);
    }
}
